﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using GrewalCRM.Entities;

namespace GrewalCRM.DAL
{
  public class UserCRM_DAL:BaseDALGrewalCRM,IBaseDALGrewalCRM
  {

    #region Constructors
		/// <summary>
		/// Create a new instance using the default constructor
		/// </summary>
    public UserCRM_DAL()
		{
      
		}      
    #endregion

    #region Public Methods
    public int Save(BaseEntitiesGrewalCRM Entity)
    {
      throw new NotImplementedException();
    }

    public override List<BaseEntitiesGrewalCRM> GetAll()
    {
      throw new NotImplementedException();
    }

    public override BaseEntitiesGrewalCRM GetByID(int ID)
    {
      throw new NotImplementedException();
    }

    public override void FillEntity(BaseEntitiesGrewalCRM Entity)
    {
      UserCRM user = Entity as UserCRM;
      Cmd.CommandText = "usp_GetCRM_UsersByCredential";
      Cmd.Parameters.AddWithValue("@LoginID", user.LoginID);
      Cmd.Parameters.AddWithValue("@Password", user.Password);
      Cmd.Connection.Open();
      SqlDataReader rdr = Cmd.ExecuteReader();
      List<BaseEntitiesGrewalCRM> allUsers = new List<BaseEntitiesGrewalCRM>();
      
      if (rdr != null && rdr.HasRows)
      {        
        while (rdr.Read())
        {          
          UserCRM selectedUser = new UserCRM(rdr[0] != null && rdr[0].GetType() != typeof(DBNull) ? rdr.GetInt32(0) : 0,                                              
                                              rdr[1] != null && rdr[1].GetType() != typeof(DBNull) ? rdr.GetString(1) : string.Empty,
                                              rdr[2] != null && rdr[2].GetType() != typeof(DBNull) ? rdr.GetString(2) : string.Empty,
                                              rdr[3] != null && rdr[3].GetType() != typeof(DBNull) ? rdr.GetInt32(3) : 0,
                                              rdr[4] != null && rdr[4].GetType() != typeof(DBNull) ? rdr.GetString(4) : string.Empty,
                                              rdr[5] != null && rdr[5].GetType() != typeof(DBNull) ? rdr.GetString(5) : string.Empty,
                                              rdr[6] != null && rdr[6].GetType() != typeof(DBNull) ? rdr.GetString(6) : string.Empty,
                                              rdr[7] != null && rdr[7].GetType() != typeof(DBNull) ? rdr.GetDateTime(7) : DateTime.MinValue,
                                              rdr[8] != null && rdr[8].GetType() != typeof(DBNull) ? rdr.GetBoolean(8) : false,
                                              new UserCRM(0),
                                              null
                                              );
          allUsers.Add(selectedUser);          
        }   
      }
      Cmd.Connection.Close();

      if (allUsers.Count > 0)
      {
          UserCRM selectedUser = Entity as UserCRM;
          selectedUser.Address = (allUsers[0] as UserCRM).Address;
          selectedUser.Doj = (allUsers[0] as UserCRM).Doj;
          selectedUser.ID = (allUsers[0] as UserCRM).ID;
          selectedUser.IsActive = (allUsers[0] as UserCRM).IsActive;
          selectedUser.LoginID = (allUsers[0] as UserCRM).LoginID;
          selectedUser.Password = (allUsers[0] as UserCRM).Password;
          selectedUser.MobileNo = (allUsers[0] as UserCRM).MobileNo;
          selectedUser.Role = (allUsers[0] as UserCRM).Role;
          selectedUser.UserName = (allUsers[0] as UserCRM).UserName;
      }
    }

    #endregion

    #region Protected Method
    public override int AddNew(BaseEntitiesGrewalCRM Entity)
    {
      throw new NotImplementedException();
    }

    public override int Update(BaseEntitiesGrewalCRM Entity)
    {
      throw new NotImplementedException();
    }

    public override int Delete(BaseEntitiesGrewalCRM Entity)
    {
      throw new NotImplementedException();
    }
    #endregion
           
  }
}
